package cn.itcast.bos.web.action.base;

import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.CourierService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Administrator on 2017/7/21.
 */
@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class CourierAction extends ActionSupport implements ModelDriven<Courier> {

    //注入service
   @Autowired
    private CourierService courierservice;
    //模型驱动
    private Courier courier = new Courier();
    //属性驱动
    private int page;
    private int rows;

    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    @Override
    public Courier getModel() {

        return courier;
    }
    //添加快递员的方法
    @Action(value="courier_save",results = {@Result(name="success",type="redirect",location = "./pages/base/courier.html")})
    public String courierSave(){
        courierservice.save(courier);
        return SUCCESS;
    }
    //无条件分页查询的方法
    @Action(value="courier_pageQuery",results = {@Result(name="success",type = "json")})
    public String pageQuery(){
        PageRequest pageable= new PageRequest(page-1,rows);
        //带条件的查询
        Specification<Courier> specification = new Specification<Courier>() {
            @Override
            //root 用于获取属性字段,criteriaQuery用于构建简单的条件查询,criteriaBuilder用于构建复杂的条件查询
            public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                //快递员工号查询
                if(StringUtils.isNotBlank(courier.getCourierNum())){
                    Predicate p1 = cb.equal(root.get("courierNum").as(String.class),courier.getCourierNum());
                    list.add(p1);
                }
                //所属公司的查询
                if(StringUtils.isNotBlank(courier.getCompany())){
                    Predicate p2=cb.like(root.get("company").as(String.class),"%"+courier.getCompany()+"%");
                    list.add(p2);
                }
                //快递员类型查询
                if(StringUtils.isNotBlank(courier.getType())){
                    Predicate p3 = cb.equal(root.get("type").as(String.class),courier.getType());
                    list.add(p3);
                }
                //取派标准查询,多表查询
                //第一个Object是当前模型类.第二个是对应的另一张表的模型类
                Join<Courier,Standard> standardRoot = root.join("standard",JoinType.INNER);
                if(courier.getStandard() !=null && StringUtils.isNotBlank(courier.getStandard().getName())){
                    Predicate p4 = cb.like(standardRoot.get("name").as(String.class),"%"+courier.getStandard().getName()+"%");
                    list.add(p4);
                }
                return cb.and(list.toArray(new Predicate[0]));
            }
        };
        Page<Courier> pageData = courierservice.pageQuery(specification,pageable);
        //将查到的结果封装到map集合中,然后压如栈顶
        Map<String ,Object> results = new HashMap<>();
        results.put("total",pageData.getNumberOfElements());
        results.put("rows",pageData.getContent());
        //压如栈顶
        ActionContext.getContext().getValueStack().push(results);
        return SUCCESS;
    }
    //逻辑删除快递员的方法
    private  String ids;

    public void setIds(String ids) {
        this.ids = ids;
    }
    @Action(value = "courier_delTag",results = {@Result(name="success",type = "redirect",location = "./pages/base/courier.html")})
    public String delTag(){
        //截取ids;
        String[] idArray = ids.split(",");
        courierservice.deltag(idArray);
        return SUCCESS;
    }
    //查询未关联定区的快递员
    @Action(value = "courier_noassocictionFixedArea",results = {@Result(name="success",type = "json")})
    public String findAllNoassociationFixedArea(){
        List<Courier> courierList= courierservice.findAllNoassociationFixedArea();
        ActionContext.getContext().getValueStack().push(courierList);
        return SUCCESS;
    }
}
